package com.fingard.dsp.bank.directbank.nbcb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/11/12.
 * 宁波银行电票查询接口类
 */
public class NBCB01ElecBill extends NBCB01Base implements IElecBillQuery {
    /**
     * 可操作票据查询
     * 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
     * 查询类型 1-申请 2-应答 3-撤回
     */
    @Override
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws  Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        retAts.respCode ="0";
        retAts.writeStart();
        try{
            while (query.readRecord()) {
                if("1".equals(query.detail.queryType)&&"2".equals(query.detail.busiType)){//承兑申请
                    retAts=queryOpenBill(query,"2");
                }else if("2".equals(query.detail.queryType)&&"2".equals(query.detail.busiType)){//承兑应答
                    retAts=queryOpenBill(query,"3");
                }else if("3".equals(query.detail.queryType)&&"1".equals(query.detail.busiType)){//撤票
                    retAts=queryOpenBill(query,"4");
                }else if ("1".equals(query.detail.queryType)) {
                    retAts=b001OperBillQryOne(query);//通用申请查询 ok
                } else if ("2".equals(query.detail.queryType)) {
                    retAts=b001OperBillQryTwo(query);//通用应答查询 ok
                }else if("3".equals(query.detail.queryType)){
                    retAts=b001OperBillQryThree(query);//通用撤回查询
                }else {
                    throw new Exception("查询类型不明：" + query.detail.queryType);
                }
            }
        }catch (Exception ex ){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode="-1";
            retAts.respInfo="查询异常";
            retAts.writeBefore();
            ex.printStackTrace();
            WriteBankLogLn("查询异常:"+ex);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据操作明细查询 不支持
     */
    @Override
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws  Exception{
        RetAtsB002BillTransQry retAts  = new RetAtsB002BillTransQry(query.reqHeader);
        retAts.respCode="-1";
        retAts.respInfo="暂不支持";
        retAts.writeStart();
        retAts.writeAfter();
        return retAts;
    }

    /**
     * 正面信息查询  ok
     */
    @Override
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query){
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo ="";
        try{
            //赋值通用参数
            serviceId = "srv3027_singleBillInfoQuery";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter);
            if(!"".equals(bankFront.token)) {
                while (query.readRecord()) {
                    xmlWriter.writeElementString("functionId", "srv173");
                    xmlWriter.writeElementString("functionName", "单笔票据查询");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("PJHM", query.detail.billNo);//票据号码
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    while(tmpBkReader.read("/NBCBEBankData/opRep/opResult")){
                                        retAts.respCode= "0";
                                        retAts.respInfo= tmpBkReader.readedXElement.getSingleTextNotNull("SBYY");
                                        retAts.billNo= tmpBkReader.readedXElement.getSingleTextNotNull("PJHM");//电子票据
                                        retAts.billType= getBillTypeToAts(tmpBkReader.readedXElement.getSingleTextNotNull("PJLX"));//票据类型 1-银承 2-商承
                                        retAts.issDate= tmpBkReader.readedXElement.getSingleTextNotNull("CPRQ");//出票日
                                        retAts.dueDate= tmpBkReader.readedXElement.getSingleTextNotNull("DQR");//到期日
                                        retAts.billAmt= tmpBkReader.readedXElement.getSingleTextNotNull("PJJE");//电子票据金额
                                        retAts.transFlag= getBankConfig().getMapKeyByValue("transFlagMapping",
                                                tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));//背书转让标识 1-可再转让 0-不得转让
                                        retAts.mustPay= "";//承兑无条件支付委托 1-无条件支付 0-非条件支付
                                        retAts.drawName= tmpBkReader.readedXElement.getSingleTextNotNull("CPRMC");//出票人
                                        retAts.drawCode= tmpBkReader.readedXElement.getSingleTextNotNull("CPRJGDM");//出票人组织代码
                                        retAts.drawAct= tmpBkReader.readedXElement.getSingleTextNotNull("CPRZH");//出票人账号
                                        retAts.drawBnkName= tmpBkReader.readedXElement.getSingleTextNotNull("CPRHM");//出票人开户行行名
                                        retAts.drawBnkCode= tmpBkReader.readedXElement.getSingleTextNotNull("CPRHH");//出票人开户行行号
                                        retAts.creditLv= "";//出票人信用等级
                                        retAts.gradeAgent= "";//出票人信用评级机构
                                        retAts.gradeDueDate= "";//出票人评级到期日
                                        retAts.drGuarName= tmpBkReader.readedXElement.getSingleTextNotNull("CPRBZRMC");//出票保证人名称
                                        retAts.drGuarAddr= tmpBkReader.readedXElement.getSingleTextNotNull("CPRBZRDZ");//出票保证人地址
                                        String drGuarDate=tmpBkReader.readedXElement.getSingleTextNotNull("CPBZRQ");//出票保证日期
                                        if("00000000".equals(drGuarDate)){
                                            drGuarDate="";
                                        }
                                        retAts.drGuarDate= drGuarDate;
                                        retAts.accpName= tmpBkReader.readedXElement.getSingleTextNotNull("CDRMC");//承兑人名称
                                        retAts.accpAct= tmpBkReader.readedXElement.getSingleTextNotNull("CDRZH");//承兑人账号
                                        retAts.accpBnkName= tmpBkReader.readedXElement.getSingleTextNotNull("CDRHM");//承兑人开户行行名
                                        retAts.accpBnkCode= tmpBkReader.readedXElement.getSingleTextNotNull("CDRHH");//承兑人开户行行号
                                        retAts.accpCredit= "";//承兑人信用等级
                                        retAts.accpGrade= "";//承兑人评级机构
                                        retAts.accpGradeDue= "";//承兑人评级到期日
                                        retAts.apGuarName= tmpBkReader.readedXElement.getSingleTextNotNull("CDBZRMC");//承兑保证人名称
                                        retAts.apGuarAddr= tmpBkReader.readedXElement.getSingleTextNotNull("CDBZRDZ");//承兑保证人地址
                                        String apGuarDate=tmpBkReader.readedXElement.getSingleTextNotNull("CDBZRQ");//承兑保证日期
                                        if("00000000".equals(apGuarDate)){
                                            apGuarDate="";
                                        }
                                        retAts.apGuarDate= apGuarDate;
                                        retAts.payeeName= tmpBkReader.readedXElement.getSingleTextNotNull("SPRMC");//收款人名称
                                        retAts.payeeAct= tmpBkReader.readedXElement.getSingleTextNotNull("SPRZH");//收款人账号
                                        retAts.payeeBnkName= tmpBkReader.readedXElement.getSingleTextNotNull("SPRHM");//收款人开户行行名
                                        retAts.payeeBnkCode= "";//收款人开户行行号
                                        retAts.billState= tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");//票据状态
                                    }
                                }else {
                                    respCode = "-1";
                                    respInfo = respInfos;
                                }
                            } else {
                                respCode = "-1";
                                respInfo = tmpSbMsg.toString();
                            }
                        } else {
                            respCode = "-1";
                            respInfo = tmpStrRet[1];
                        }
                    }else {
                        respCode = "-1";
                        respInfo = tmpSign[1];
                    }
                }
            }else {
                respCode = "-1";
                respInfo = "登录失败";
            }
        }catch (Exception e){
            respCode = "-1";
            respInfo ="查询票据正面信息异常："+e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询票据正面信息异常："+e.getMessage());
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB004BillFrontQry(query.reqHeader);
            retAts.writeStart();
            retAts.respCode =respCode;
            retAts.respInfo = respInfo;
        }
        retAts.writeRetDetail();
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据背面信息查询  ok
     */
    @Override
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception{
        RetAtsB005BillBackQry retAts = new RetAtsB005BillBackQry(query.reqHeader);
        retAts.writeStart();
        String respCode = "";
        String respInfo ="";
        try {
            //赋值通用参数
            serviceId = "srv3027_singleBillInfoQuery";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter);
            if (!"".equals(bankFront.token)) {
                while (query.readRecord()) {
                    xmlWriter.writeElementString("functionId", "srv3027");
                    xmlWriter.writeElementString("functionName", "单笔票据查询");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("PJHM", query.detail.billNo);//票据号码
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")) {
                                        RetAtsB005BillBackQryDetail detail = new RetAtsB005BillBackQryDetail();
                                        detail.respCode = "0";
                                        detail.respInfo = "";
                                        detail.billNo = query.detail.billNo;//电子票据
                                        detail.transCode = Integer.parseInt(tmpBkReader.readedXElement.getSingleTextNotNull("YWLX"))+"";//交易类型代码  背书010
                                        detail.applyName = tmpBkReader.readedXElement.getSingleTextNotNull("SQRMC");//申请人
                                        detail.receName = tmpBkReader.readedXElement.getSingleTextNotNull("JSRMC");//接收人
                                        detail.applyDate = tmpBkReader.readedXElement.getSingleTextNotNull("RQ1");//申请日期
                                        detail.respDate = "";//操作日期
                                        detail.transFlag = getBankConfig().getMapKeyByValue("transFlagMapping",
                                                tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));//背书转让标识 1-可再转让 0-不得转让
                                        detail.guarAddr = tmpBkReader.readedXElement.getSingleTextNotNull("BZDZ");//保证人地址
                                        detail.repOpenDate = "";//回购开放日
                                        detail.repDueDate = "";//回购截止日期
                                        detail.respFlag = "";//应答标记 1--签收 2--驳回
                                        detail.unPayCode = tmpBkReader.readedXElement.getSingleTextNotNull("ZSLX");//拒付理由代码 1--DC00 2--DC01 N--DC0(N-1) 99--DC09
                                        detail.recourseType = "";//追索类型 1-拒付追索 2-非拒付追索
                                        retAts.writeDetail(detail);
                                    }
                                } else {
                                    retAts.writeRespCode("-1");
                                    retAts.writeRespInfo(respInfos);
                                }
                            } else {
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo(tmpSbMsg.toString());
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpStrRet[1]);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSign[1]);
                    }
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo("登录失败");
            }
        }catch (Exception e){
            respCode = "-1";
            respInfo ="查询票据背面信息异常："+e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询票据背面信息异常："+e.getMessage());
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB005BillBackQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(respCode);
            retAts.writeRespCode(respInfo);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 票据交易状态查询
     */
    @Override
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query)throws  Exception{
        RetAtsB006BillStateQry retAts  = new RetAtsB006BillStateQry(query.reqHeader);
        RetAtsB006BillStateQryDetail detail=new RetAtsB006BillStateQryDetail();
        retAts.writeStart();
        try{
            //赋值通用参数
            serviceId = "srv008_transferResultInfoQuery";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            while (query.readRecord()) {
                if (!"".equals(bankFront.token)) {
                    xmlWriter.writeElementString("functionId", "srv008");
                    xmlWriter.writeElementString("functionName", "转账结果查询");
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", query.detail.reqSeqID);
                    xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("JYXH", query.detail.reqSeqID);
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);//获取签名数据
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//进行签名
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String returnCode = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (returnCode != null && isActiveRetCode(returnCode)) {
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult")) {
                                        detail.respCode = "0";//响应码
                                        detail.respInfo = tmpBkReader.readedXElement.getSingleText("SBYY");//响应信息
                                        detail.reqSeqID = query.detail.reqSeqID;//交易流水号
                                        detail.billNo = query.detail.billNo;//电子票据号码
                                        //90：交易成功 91：交易失败 94 95 96：银行处理中；99：异常
                                        detail.transState = tmpBkReader.readedXElement.getSingleText("JYZT");//处理状态码
                                        detail.payInfoCode = detail.transState;//交易信息码
                                        detail.billState = getBillStatusByBillNo(detail.billNo);//票据状态
                                    }
                                }else {
                                    detail.respCode = "-1";
                                    detail.respInfo = respInfos;
                                }
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = tmpSbMsg.toString();
                            }
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = tmpStrRet[1];
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSign[1];
                    }
                }else {
                    detail.respCode = "-1";
                    detail.respInfo = "登录失败";
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
            WriteBankLogLn("票据交易状态查询异常：" + ex);
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 可追索人查询 不支持
     */
    @Override
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception{
        RetAtsB007BillKzsrQry retAts=new RetAtsB007BillKzsrQry(query.reqHeader);
        retAts.writeStart();
        retAts.respCode="-1";
        retAts.respInfo="银行暂不支持此操作";
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 历史票据查询
     */
    @Override
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception{
        RetAtsB008HistoricalBillQry retAts = new RetAtsB008HistoricalBillQry(query.reqHeader);
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        query.readRecord();
        try{
            //赋值通用参数
            serviceId = "srv3029_showEndorsementHistory";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter);
            if(!"".equals(bankFront.token)) {
                    xmlWriter.writeElementString("functionId", "srv3029");
                    xmlWriter.writeElementString("functionName", "查询背书历史信息");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("PJHM", query.detail.billNo);//票据号码
                    xmlWriter.writeElementString("PJLX", getBillTypeToBank(query.detail.billType));//票据类型
                    xmlWriter.writeElementString("ZJHM", bankFront.org);//证件号码
                    xmlWriter.writeElementString("KHHM", getBankConfig().getValueAsString("customerID"));//客户号码
                    if(!"2".equals(query.detail.busiType)||"2".equals(query.detail.queryType)){
                        throw new Exception("银行只支持查询【背书转让】业务历史票据查询");
                    }
                    xmlWriter.writeElementString("YWLX", "010");//业务类型
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    if(tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/list/row")!=null){
                                        while(tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")){
                                            RetAtsB008HistoricalBillQryDetail detail=new RetAtsB008HistoricalBillQryDetail();
                                            detail.billNo = query.detail.billNo;//票据号
                                            detail.billType = query.detail.billType;//票据类型
                                            detail.issDate = tmpBkReader.readedXElement.getSingleText("SQRQ");//申请日期
                                            detail.dueDate = tmpBkReader.readedXElement.getSingleText("YWQSRQ");//到期日
                                            detail.transFlag=getBankConfig().getMapKeyByValue("transFlagMapping",tmpBkReader.readedXElement.getSingleText("ZRBJ"));//不可转让标识
                                            detail.drawName = tmpBkReader.readedXElement.getSingleText("FQRMC");//出票人
                                            detail.drawCode = "";//出票人组织代码
                                            detail.drawAct = tmpBkReader.readedXElement.getSingleText("FQRZH");//出票人账户
                                            detail.drawBnkName = tmpBkReader.readedXElement.getSingleText("FQRHM");//出票人开户行行名
                                            detail.payeeName = tmpBkReader.readedXElement.getSingleText("SPRMC");//收款人名
                                            detail.payeeCode = "";
                                            detail.payeeAct = tmpBkReader.readedXElement.getSingleText("SPRZH");//收款人账户
                                            detail.payeeBnkName = tmpBkReader.readedXElement.getSingleText("SPRHM");//收款人行名
                                            retAts.writeDetail(detail);
                                        }
                                    }else {
                                        retAts.respCode = "0";
                                        retAts.respInfo = "未查询到相关票据信息";
                                    }
                                } else if("S116".equals(respCodes)){
                                    retAts.respCode = "0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                }else {
                                    retAts.respCode="-1";
                                    retAts.respInfo=respInfos;
                                }
                            } else {
                                retAts.respCode="-1";
                                retAts.respInfo=tmpSbMsg.toString();
                            }
                        } else {
                            retAts.respCode="-1";
                            retAts.respInfo=tmpStrRet[1];
                        }
                    }else {
                        retAts.respCode="-1";
                        retAts.respInfo=tmpSign[1];
                    }
            }else {
                retAts.respCode="-1";
                retAts.respInfo="登录失败";
            }
        }catch (Exception e){
            retAts.respCode="-1";
            retAts.respInfo=e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询历史票据异常");
        }
        retAts.writeAfter();
        return retAts;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }

    /**
     * 通用申请查询 ok
     * @param query
     * @return
     * @throws Exception
     */
    private RetAtsB001OperBillQry b001OperBillQryOne(ReqB001OperBillQry query)throws Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        try {
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            int totalNum = -1;
            do {
                //赋值通用参数
                serviceId = "srv3004_applyCommonQuery";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                if (!"".equals(bankFront.token)) {
                    //查询条件转换
                    String eBillBusessType = getBillType(query.detail.busiType, query.detail.overdueFlag);

                    xmlWriter.writeElementString("functionId", "srv3004");
                    xmlWriter.writeElementString("functionName", "通用申请查询");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("CXTJ", eBillBusessType);//查询条件
                    xmlWriter.writeElementString("KHHM", getBankConfig().getValueAsString("customerID"));//实际交易客户号
                    xmlWriter.writeElementString("ZH", query.detail.actNum);//出票账号
                    xmlWriter.writeElementClosed("ZZH");//子账号
                    xmlWriter.writeElementClosed("PJHM");//票据号码
                    xmlWriter.writeElementString("PJZL", getBillTypeToBank(query.detail.billType));//票据类型
                    xmlWriter.writeElementString("SQQR", "");//开始时间
                    xmlWriter.writeElementString("SQZR", "");//结束时间
                    xmlWriter.writeElementString("XSYH", tmpNowReadStart + "");//页号
                    xmlWriter.writeElementString("XSTS", getBankConfig().getValueAsString("pageSize"));//每页数量
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行

                        //测试
//                    tmpStrRet[0]="";
//                    tmpStrRet[1]="<?xml version=“1.0” encoding=“UTF-8”?><NBCBEBankData><serialNo>12345678901234567890</serialNo><sessionId>1111111111111</sessionId><retCode>0000</retCode><errorMsg>成功</errorMsg><opRep>\t<opResult>\t\t<YDDM>0000</YDDM>\t\t<SBYY></SBYY>\t\t<totalNumber>2</totalNumber>\t\t<list>\t\t\t<row>\t\t\t\t<PJZL>107</PJZL>\t\t\t\t<KHWD>杭州</KHWD>\t\t\t\t<PJHM>999999999999</PJHM>\t\t\t\t<PJJE>100.00</PJJE>\t\t\t\t<CPRQ>20181116</CPRQ>\t\t\t\t<DQR>20181216</DQR>\t\t\t\t<ZRBJ>EM00</ZRBJ>\t\t\t\t<BWSJ>16:21:00</BWSJ>\t\t\t\t<BWBZH>222222222</BWBZH>\t\t\t\t<PJZT>30006</PJZT>\t\t\t\t<JYLB>3</JYLB>\t\t\t\t<CPRLB>RC04</CPRLB>\t\t\t\t<CPRZH>2222222222222222222222</CPRZH>\t\t\t\t<CPRHM>宁波银行</CPRHM>\t\t\t\t<CPRMC>出票人</CPRMC>\t\t\t\t<DZQM></DZQM>\t\t\t\t<SPRHH>000000</SPRHH>\t\t\t\t<SPRHM>宁波银行</SPRHM>\t\t\t\t<SPRZH>333333333333333333333</SPRZH>\t\t\t\t<SPRMC>收票人</SPRMC>\t\t\t\t<BSCS>0</BSCS>\t\t\t\t<ZSBZ></ZSBZ>\t\t\t\t<ZSJE></ZSJE>\t\t\t\t<CPRHH>0000000</CPRHH>\t\t\t</row>\t\t\t<row>\t\t\t\t<PJZL>107</PJZL>\t\t\t\t<KHWD>杭州</KHWD>\t\t\t\t<PJHM>999999999999</PJHM>\t\t\t\t<PJJE>100.00</PJJE>\t\t\t\t<CPRQ>20181116</CPRQ>\t\t\t\t<DQR>20181216</DQR>\t\t\t\t<ZRBJ>EM00</ZRBJ>\t\t\t\t<BWSJ>16:22:00</BWSJ>\t\t\t\t<BWBZH>222222223</BWBZH>\t\t\t\t<PJZT>30006</PJZT>\t\t\t\t<JYLB>3</JYLB>\t\t\t\t<CPRLB>RC04</CPRLB>\t\t\t\t<CPRZH>2222222222222222222222</CPRZH>\t\t\t\t<CPRHM>宁波银行</CPRHM>\t\t\t\t<CPRMC>出票人</CPRMC>\t\t\t\t<DZQM></DZQM>\t\t\t\t<SPRHH>000000</SPRHH>\t\t\t\t<SPRHM>宁波银行</SPRHM>\t\t\t\t<SPRZH>333333333333333333333</SPRZH>\t\t\t\t<SPRMC>收票人</SPRMC>\t\t\t\t<BSCS>0</BSCS>\t\t\t\t<ZSBZ></ZSBZ>\t\t\t\t<ZSJE></ZSJE>\t\t\t\t<CPRHH>0000000</CPRHH>\t\t\t</row>\t\t</list>\t</opResult></opRep></NBCBEBankData>\n";
//                    WriteBankLogLn("模拟银行返回："+tmpStrRet[1]);

                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    if (totalNum == -1) {
                                        String totalCount = tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/totalNumber");
                                        //获取总记录数
                                        totalNum = Integer.valueOf(totalCount);
                                        if ("0".equals(totalCount)) {
                                            retAts.respCode = "0";
                                            retAts.respInfo = "未查询到相关票据信息";
                                            break;
                                        }
                                    }
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")) {
                                        tmpNowReadNum++;
                                        String ownAct = tmpBkReader.readedXElement.getSingleTextNotNull("CHPRZH");
                                        if (!query.detail.actNum.equals(ownAct)) {
                                            continue;
                                        }
                                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                        //电子票据号码
                                        detail.billNo = tmpBkReader.readedXElement.getSingleTextNotNull("PJHM");
                                        //票据类型 1-银承 2-商承
                                        detail.billType = getBillTypeToAts(tmpBkReader.readedXElement.getSingleTextNotNull("PJZL"));
                                        //出票日(yyyy-MM-dd)
                                        detail.issDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("CPRQ"));
                                        //到期日(yyyy-MM-dd)
                                        detail.dueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("DQR"));
                                        //电子票据金额
                                        detail.billAmt = tmpBkReader.readedXElement.getSingleTextNotNull("PJJE");
                                        //背书转让标识 1-可再转让 0-不得转让
                                        detail.transFlag = getBankConfig().getMapKeyByValue("transFlagMapping", tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));

                                        //出票人
                                        detail.drawName = tmpBkReader.readedXElement.getSingleTextNotNull("CPRMC");
                                        detail.drawAct = tmpBkReader.readedXElement.getSingleTextNotNull("CPRZH");
                                        detail.drawBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("CPRHM");
                                        detail.drawBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("CPRHH");
                                        detail.drawCode = tmpBkReader.readedXElement.getSingleText("CPRJGDM");

                                        //承兑人
                                        detail.accpName = tmpBkReader.readedXElement.getSingleTextNotNull("CDRMC");
                                        detail.accpAct = tmpBkReader.readedXElement.getSingleTextNotNull("CDZH");
                                        detail.accpBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("CDRHM");
                                        detail.accpBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("CDRHH");

                                        //收款人
                                        detail.payeeName = tmpBkReader.readedXElement.getSingleTextNotNull("SPRMC");
                                        detail.payeeAct = tmpBkReader.readedXElement.getSingleTextNotNull("SPRZH");
                                        detail.payeeBnkName = tmpBkReader.readedXElement.getSingleTextNotNull("SPRHM");
                                        detail.payeeBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("SPRHH");
                                        //交易明细流水
                                        detail.busiNo = tmpBkReader.readedXElement.getSingleTextNotNull("PJLSH");
                                        //交易合同号码
                                        detail.contractNo = tmpBkReader.readedXElement.getSingleTextNotNull("JYHT");
                                        //发票号码
                                        detail.invoiceNo = tmpBkReader.readedXElement.getSingleTextNotNull("FPHM");
                                        //票据状态
                                        detail.billStatus = tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");
                                        retAts.writeDetail(detail);
                                    }
                                    tmpNowReadStart++;//页数+1
                                } else if("S116".equals(respCodes)){
                                    retAts.respCode = "0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                } else {
                                    respCode="-1";
                                    respInfo=respInfos;
                                    break;
                                }
                            } else {
                                respCode="-1";
                                respInfo=tmpSbMsg.toString();
                                break;
                            }
                        } else {
                            respCode="-1";
                            respInfo=tmpStrRet[1];
                            break;
                        }
                    } else {
                        respCode="-1";
                        respInfo=tmpSign[1];
                        break;
                    }
                } else {
                    respCode="-1";
                    respInfo="登录失败";
                    break;
                }
            } while (tmpNowReadNum < totalNum);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 通用签收查询 ok
     * @param query
     * @return
     * @throws Exception
     */
    private RetAtsB001OperBillQry b001OperBillQryTwo(ReqB001OperBillQry query)throws Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        try {
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            int totalNum = -1;
            do {
                //查询条件转换
                String eBillBusessType = getBillType(query.detail.busiType, query.detail.overdueFlag);
                //赋值通用参数
                serviceId = "srv3005_signCommonQuery";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                if (!"".equals(bankFront.token)) {
                    xmlWriter.writeElementString("functionId", "srv3005");
                    xmlWriter.writeElementString("functionName", "通用签收查询");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("CXTJ", eBillBusessType);//查询条件
                    xmlWriter.writeElementString("KHHM", getBankConfig().getValueAsString("customerID"));//实际交易客户号"9000016229"、"0000163779"
                    xmlWriter.writeElementString("ZH", query.detail.actNum);//出票账号
                    xmlWriter.writeElementString("FZH", "");//子账号
                    xmlWriter.writeElementString("PJHM", "");//票据号码
                    xmlWriter.writeElementString("PJZL", getBillTypeToBank(query.detail.billType));//票据类型
                    xmlWriter.writeElementString("SQQR", "");//开始时间
                    xmlWriter.writeElementString("SQZR", "");//结束时间
                    xmlWriter.writeElementString("XSYH", tmpNowReadStart + "");//页号
                    xmlWriter.writeElementString("XSTS", getBankConfig().getValueAsString("pageSize"));//每页数量
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    if (totalNum == -1) {
                                        String totalCount = tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/totalNumber");
                                        //获取总记录数
                                        totalNum = Integer.valueOf(totalCount);
                                        if ("0".equals(totalCount)) {
                                            retAts.respCode = "0";
                                            retAts.respInfo = "未查询到相关票据信息";
                                            break;
                                        }
                                    }
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")) {
                                        tmpNowReadNum++;
                                        /*String singAct = tmpBkReader.readedXElement.getSingleTextNotNull("JSRZH");
                                        if (!query.detail.actNum.equals(singAct)) {
                                            continue;
                                        }*/
                                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                        //电子票据号码
                                        detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("PJHM");
                                        //票据类型 1-银承 2-商承
                                        detail.billType=getBillTypeToAts(tmpBkReader.readedXElement.getSingleTextNotNull("PJZL"));
                                        //出票日(yyyy-MM-dd)
                                        detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("CPRQ"));
                                        //到期日(yyyy-MM-dd)
                                        detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("DQR"));
                                        //电子票据金额
                                        detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("PJJE");
                                        //背书转让标识 1-可再转让 0-不得转让
                                        detail.transFlag=getBankConfig().getMapKeyByValue("transFlagMapping",tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));
                                        //出票人
                                        detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRMC");
                                        detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("CPRZH");
                                        detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHM");
                                        detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHH");
                                        //承兑人
                                        detail.accpName=tmpBkReader.readedXElement.getSingleTextNotNull("CDRMC");
                                        detail.accpAct=tmpBkReader.readedXElement.getSingleTextNotNull("CDZH");
                                        detail.accpBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("CDRHM");
                                        detail.accpBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("CDRHH");
                                        //收款人
                                        detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRMC");
                                        detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("SPRZH");
                                        detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHM");
                                        detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHH");
                                        //交易明细流水
                                        detail.busiNo=tmpBkReader.readedXElement.getSingleTextNotNull("BWBSH");
                                        //申请人
                                        detail.applyName=tmpBkReader.readedXElement.getSingleTextNotNull("FQRMC");
                                        detail.applyAct=tmpBkReader.readedXElement.getSingleTextNotNull("FQRZH");
                                        detail.applyBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("FQRHM");
                                        detail.applyBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("FQRHH");
                                        //接收人
                                        detail.receName=tmpBkReader.readedXElement.getSingleTextNotNull("JSRMC");
                                        detail.receAct=tmpBkReader.readedXElement.getSingleTextNotNull("JSRZH");
                                        detail.receBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("JSRHM");
                                        detail.receBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("JSRHH");
                                        //票据状态
                                        String billStatus=tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");
                                        if(StringHelper.hasAnyChar(billStatus) && billStatus.length()!=6){
                                            billStatus=Integer.parseInt(billStatus)+"";
                                        }
                                        if(billStatus.length()!=6){
                                            billStatus=getBankConfig().getMapValueByKey("billStatusMapping",billStatus);
                                        }
                                        if(StringHelper.isNullOrEmpty(billStatus)){//未关联上票据状态，再通过单笔票据信息查询一次
                                           billStatus = getBillStatusByBillNo(detail.billNo);//票据状态
                                        }
                                        detail.billStatus=billStatus;
                                        retAts.writeDetail(detail);
                                    }
                                    tmpNowReadStart++;//页数+1
                                } else if("S116".equals(respCodes)){
                                    retAts.respCode = "0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                } else {
                                    respCode="-1";
                                    respInfo=respInfos;
                                    break;
                                }
                            } else {
                                respCode="-1";
                                respInfo=tmpSbMsg.toString();
                                break;
                            }
                        } else {
                            respCode="-1";
                            respInfo=tmpStrRet[1];
                            break;
                        }
                    } else {
                        respCode="-1";
                        respInfo=tmpSign[1];
                        break;
                    }
                } else {
                    respCode="-1";
                    respInfo="登录失败";
                    break;
                }
            } while (tmpNowReadNum < totalNum);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 撤回通用查询
     * @param query
     * @return
     * @throws Exception
     */
    private RetAtsB001OperBillQry b001OperBillQryThree(ReqB001OperBillQry query)throws Exception{
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        try {
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            int totalNum = -1;
            do {
                //赋值通用参数
                if("12".equals(query.detail.busiType)){
                    serviceId="srv3034_remindPayCancle";
                }else {
                    serviceId = "srv3015_revokeCommonQuery";
                }
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                if (!"".equals(bankFront.token)) {
                    //查询条件转换
                    String eBillBusessType = getBillType(query.detail.busiType, query.detail.overdueFlag);

                    if("12".equals(query.detail.busiType)){
                        xmlWriter.writeElementString("functionId", "srv3034");
                        xmlWriter.writeElementString("functionName", "提示付款撤销查询");
                    }else {
                        xmlWriter.writeElementString("functionId", "srv3015");
                        xmlWriter.writeElementString("functionName", "撤回通用查询");
                    }
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    if("12".equals(query.detail.busiType)){
                        xmlWriter.writeElementString("CXTJ", query.detail.overdueFlag);//查询条件
                    }else {
                        xmlWriter.writeElementString("CXTJ", eBillBusessType);//查询条件
                    }
                    xmlWriter.writeElementString("KHHM", getBankConfig().getValueAsString("customerID"));//实际交易客户号bankFront.loginName
                    xmlWriter.writeElementString("ZH", query.detail.actNum);//出票账号
                    xmlWriter.writeElementString("FZH", "");//子账号
                    xmlWriter.writeElementString("PJHM", query.detail.billNo);//票据号码
                    xmlWriter.writeElementString("PJZL", getBillTypeToBank(query.detail.billType));//票据类型
                    xmlWriter.writeElementString("CPQR", "");//开始时间
                    xmlWriter.writeElementString("CPZR", "");//结束时间
                    xmlWriter.writeElementString("XSYH", tmpNowReadStart + "");//页号
                    xmlWriter.writeElementString("XSTS", getBankConfig().getValueAsString("pageSize"));//每页数量
                    if("12".equals(query.detail.busiType)){
                        xmlWriter.writeElementString("DQQR", query.detail.dueDateDown);//到期日下限
                        xmlWriter.writeElementString("DQZR", query.detail.dueDateUp);//到期日上限
                    }
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    if (totalNum == -1) {
                                        String totalCount = tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/totalNumber");
                                        //获取总记录数
                                        totalNum = Integer.valueOf(totalCount);
                                        if ("0".equals(totalCount)) {
                                            retAts.respCode = "0";
                                            retAts.respInfo = "未查询到相关票据信息";
                                            break;
                                        }
                                    }
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")) {
                                        tmpNowReadNum++;
                                        /*String singAct = tmpBkReader.readedXElement.getSingleTextNotNull("JSRZH");
                                        if (!query.detail.actNum.equals(singAct)) {
                                            continue;
                                        }*/
                                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                        //电子票据号码
                                        detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("PJHM");
                                        //票据类型 1-银承 2-商承
                                        detail.billType=getBillTypeToAts(tmpBkReader.readedXElement.getSingleTextNotNull("PJZL"));
                                        //出票日(yyyy-MM-dd)
                                        detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("CPRQ"));
                                        //到期日(yyyy-MM-dd)
                                        detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("DQR"));
                                        //电子票据金额
                                        detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("PJJE");
                                        //背书转让标识 1-可再转让 0-不得转让
                                        detail.transFlag=getBankConfig().getMapKeyByValue("transFlagMapping",tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));
                                        //出票人
                                        detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRMC");
                                        detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("CPRZH");
                                        detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHM");
                                        detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHH");

                                        //收款人
                                        detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRMC");
                                        detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("SPRZH");
                                        detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHM");
                                        detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHH");
                                        //追索类型 1-拒付追索 2-非拒付追索
    //                                    detail.recourseType=tmpBkReader.readedXElement.getSingleTextNotNull("ZSBZ");
                                        //追索金额
                                        detail.recourseAmt=tmpBkReader.readedXElement.getSingleTextNotNull("ZSJE");
                                        //追索理由代码（非拒付时） 1-承兑人被依法宣告破产 2-承兑人因违法被责令终止活动
    //                                    detail.recourseCode="";
                                        //票据状态
                                        if("12".equals(query.detail.busiType)){
                                            String billStatus=tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");//返回状态为1正常 2逾期
                                            if(StringHelper.hasAnyChar(billStatus) && billStatus.length()!=6){
                                                billStatus=Integer.parseInt(billStatus)+"";
                                            }
                                            detail.billStatus=getBankConfig().getMapValueByKey("billStatus2Mapping",billStatus);
                                            if(StringHelper.isNullOrEmpty(detail.billStatus)){//未关联上票据状态，再通过单笔票据信息查询一次
                                                detail.billStatus = getBillStatusByBillNo(detail.billNo);//票据状态
                                            }
                                        }else {
                                            detail.billStatus=tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");
                                        }
                                        retAts.writeDetail(detail);
                                    }
                                    tmpNowReadStart++;//页数+1
                                } else if("S116".equals(respCodes)){
                                    retAts.respCode = "0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                } else {
                                    respCode="-1";
                                    respInfo=respInfos;
                                    break;
                                }
                            } else {
                                respCode="-1";
                                respInfo=tmpSbMsg.toString();
                                break;
                            }
                        } else {
                            respCode="-1";
                            respInfo=tmpStrRet[1];
                            break;
                        }
                    } else {
                        respCode="-1";
                        respInfo=tmpSign[1];
                        break;
                    }
                } else {
                    respCode="-1";
                    respInfo="登录失败";
                    break;
                }
            } while (tmpNowReadNum < totalNum);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 承兑申请、出票撤回 未测
     * @param query
     * @param queryType
     * @return
     */
    private RetAtsB001OperBillQry queryOpenBill(ReqB001OperBillQry query, String queryType) {
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode ="";
        String respInfo ="";
        retAts.respCode = "0";
        retAts.respInfo  = "";
        retAts.writeBefore();
        try {
            int tmpNowReadStart = 1;//起始页数
            int tmpNowReadNum = 0;//已查询笔数
            int totalNum = -1;
            do {
                //赋值通用参数
                serviceId = "srv3130_bankTicketInfoQuery";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                if (!"".equals(bankFront.token)) {
                    xmlWriter.writeElementString("functionId", "srv3130");
                    xmlWriter.writeElementString("functionName", "出票信息查询");
                    //请求报文体
                    xmlWriter.writeStartElement("opReq");
                    xmlWriter.writeElementString("serialNo", getQryTransID());//交易序列号
                    xmlWriter.writeElementString("reqTime", Format.Date8Format.format(new Date()));//客户端请求时间
                    xmlWriter.writeStartElement("ReqParam");
                    xmlWriter.writeElementString("KHHM", getBankConfig().getValueAsString("customerID"));//实际交易客户号
                    xmlWriter.writeElementString("CPQR", "");//开始时间
                    xmlWriter.writeElementString("CPZR", "");//结束时间
                    xmlWriter.writeElementString("CXTJ", queryType);//查询条件
                    xmlWriter.writeElementString("ZH", query.detail.actNum);//出票账号
                    xmlWriter.writeElementString("FZH", "");//子账号
                    xmlWriter.writeElementString("CZH", "");//出账号
                    xmlWriter.writeElementString("PJHM","");//票据号码
                    xmlWriter.writeElementString("PJZL", getBillTypeToBank(query.detail.billType));//票据类型
                    xmlWriter.writeElementString("XSYH", tmpNowReadStart + "");//页号
                    xmlWriter.writeElementString("XSTS", getBankConfig().getValueAsString("pageSize"));//每页数量
                    xmlWriter.writeEndElement();
                    xmlWriter.writeEndElement();
                    String signXml = getDataSignData(xmlWriter);
                    if (signnatrue(signXml) == true) {
                        xmlWriter.writeElementString("signData", signature);//签名数据
                        xmlWriter.writeEndDocument();
                        String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                        if (tmpStrRet[0].length() == 0) {
                            StringBuilder tmpSbMsg = new StringBuilder();
                            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                            if (tmpBkReader != null) {
                                String respCodes = tmpBkReader.getFirstText("/NBCBEBankData/retCode");
                                String respInfos = tmpBkReader.getFirstText("/NBCBEBankData/errorMsg");
                                if (respCodes != null && isActiveRetCode(respCodes)) {
                                    if (totalNum == -1) {
                                        String totalCount = tmpBkReader.getFirstText("/NBCBEBankData/opRep/opResult/totalNumber");
                                        //获取总记录数
                                        totalNum = Integer.valueOf(totalCount);
                                        if ("0".equals(totalCount)) {
                                            retAts.respCode = "0";
                                            retAts.respInfo = "未查询到相关票据信息";
                                            break;
                                        }
                                    }
                                    while (tmpBkReader.read("/NBCBEBankData/opRep/opResult/list/row")) {
                                        tmpNowReadNum++;
                                        String singAct = tmpBkReader.readedXElement.getSingleTextNotNull("JSRZH");
                                        if (!query.detail.actNum.equals(singAct)) {
                                            continue;
                                        }
                                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                                        //电子票据号码
                                        detail.billNo=tmpBkReader.readedXElement.getSingleTextNotNull("PJHM");
                                        //票据类型 1-银承 2-商承
                                        detail.billType=getBillTypeToAts(tmpBkReader.readedXElement.getSingleTextNotNull("PJZL"));
                                        //出票日(yyyy-MM-dd)
                                        detail.issDate= DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("CPRQ"));
                                        //到期日(yyyy-MM-dd)
                                        detail.dueDate=DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleTextNotNull("DQR"));
                                        //电子票据金额
                                        detail.billAmt=tmpBkReader.readedXElement.getSingleTextNotNull("PJJE");
                                        //背书转让标识 1-可再转让 0-不得转让
                                        detail.transFlag=getBankConfig().getMapKeyByValue("transFlagMapping",tmpBkReader.readedXElement.getSingleTextNotNull("ZRBJ"));
                                        //出票人
                                        detail.drawName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRMC");
                                        detail.drawAct=tmpBkReader.readedXElement.getSingleTextNotNull("CPRZH");
                                        detail.drawBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHM");
                                        detail.drawBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("CPRHH");
                                        //承兑人
                                        detail.accpName=tmpBkReader.readedXElement.getSingleTextNotNull("CDRMC");
                                        detail.accpAct=tmpBkReader.readedXElement.getSingleTextNotNull("CDRZH");
                                        detail.accpBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("CDRHM");
                                        detail.accpBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("CDRHH");
                                        //收款人
                                        detail.payeeName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRMC");
                                        detail.payeeAct=tmpBkReader.readedXElement.getSingleTextNotNull("SPRZH");
                                        detail.payeeBnkName=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHM");
                                        detail.payeeBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("SPRHH");
                                        //票据状态
                                        detail.billStatus=tmpBkReader.readedXElement.getSingleTextNotNull("PJZT");
                                        retAts.writeDetail(detail);
                                    }
                                    tmpNowReadStart++;//页数+1
                                } else if("S116".equals(respCodes)){
                                    retAts.respCode = "0";
                                    retAts.respInfo = "未查询到相关票据信息";
                                    break;
                                }else {
                                    respCode="-1";
                                    respInfo=respInfos;
                                    break;
                                }
                            } else {
                                respCode="-1";
                                respInfo=tmpSbMsg.toString();
                                break;
                            }
                        } else {
                            respCode="-1";
                            respInfo=tmpStrRet[1];
                            break;
                        }
                    } else {
                        respCode="-1";
                        respInfo=tmpSign[1];
                        break;
                    }
                } else {
                    respCode="-1";
                    respInfo="登录失败";
                    break;
                }
            } while (tmpNowReadNum < totalNum);
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            retAts.writeBefore();
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode = "-1";
            retAts.respInfo  = respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }
}
